<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<style type="text/css">
	.right-operation ul{ list-style: none; }
	.right-operation ul li{ margin-top:10px;margin-bottom:10px; }
	.inputbtn{ height:35px;width:90px; }
	.td1{ width:10%; }
	.td2{ width:60%; }
	.td3{ width:10%; }
	.td4{ width:20%; }
	.self{ background-color:RGB(233,233,233); }
	.emz{ color:red; }
</style>

<div class="right-panel">
	<div class="header">
		<h1>文章编辑页面</h1>
	</div>
	
	<div class="right-operation">
		<h2>文章编辑</h2>
		<table>
			<thead>
				<tr>
					<td class="td1">文章编号</td>
					<td class="td2">文章标题</td>
					<td class="td3">状态</td>
					<td class="td4">操作</td>
				</tr>
			</thead>
			<tbody>
				<c:forEach items="${data}" var="a" varStatus="s">
				<c:set var="clz" value="${a.ops == 1 ? 'self' : '' }"></c:set>
				<tr class="${clz }">
					<td class="td1" id="key${s.count}">${a._id }</td>
					<td class="td2"><a href="/article/${a.stitle }" title="预览:${a.title }" target="_blank">${a.stitle }(${a.author })</a></td>
					<c:choose>
						<c:when test="${a.ops == 1 }"><td class="td3"><input type="button" value="公开" class="inputbtn emz" data-ops="public" data-index="${s.count}"/></td></c:when>
						<c:when test="${a.ops == 0 }"><td class="td3"><input type="button" value="私有" class="inputbtn" data-ops="self" data-index="${s.count}"/></td></c:when>
					</c:choose>
					<td class="td4"><input type="button" value="修改" class="inputbtn" data-ops="go" data-index="http://${server }/admin_t/index?p=newArticle&id=${a._id }"/><input type="button" value="删除" class="inputbtn" data-ops="delete" data-index="${s.count}"/></td>
				</tr>
				</c:forEach>
			</tbody>
		</table>
	</div>
	
</div>
<script type="text/javascript">
	(function(){
		var _ops = function(ops,index){
			var k = $("#key" + index).html();
			var v = k;
			var url = "/admin_t/operation?p=archive&t=" + ops;
			$.ajax({
				url:url,
				cache:false,
				data:"k=" + encodeURIComponent(k) + "&v=" + encodeURIComponent(v),
				dataType:"json",
				success:function(json){
					if( json && json.code == 200 ){
						window.location.reload(false);
					}else{
						alert("操作失败,错误代码:" + json.msg);
					}
				},
				error:function(){
					alert("操作失败!");
				}
			});
		}
		
		var init = function(){
			$(".inputbtn").each(function(){
				var t = $(this);
				var ops = t.attr("data-ops");
				var index = t.attr("data-index");
				if( !ops || ops.length == 0 || !index || index.length == 0){
					return ;
				}
				if( "go" == ops ){
					t.click(function(){
						window.location.href = index;
					});
					return ;
				}
				t.click(function(){
					_ops(ops,index);
				});
			});
		}
		init();
	})();
</script>